//package com.perfma.xtest.tool;
//
//import com.alibaba.excel.metadata.CellData;
//import com.alibaba.excel.metadata.Head;
//import com.alibaba.excel.write.merge.AbstractMergeStrategy;
//import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
//import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
//import org.apache.poi.ss.usermodel.Cell;
//import org.apache.poi.ss.usermodel.CellType;
//import org.apache.poi.ss.usermodel.Sheet;
//import org.apache.poi.ss.util.CellRangeAddress;
//
//import java.util.List;
//
///**
// * @author: junhui.si
// * @date: 2020-10-10 10:27
// **/
//public class MyMergeStrategy extends AbstractMergeStrategy {
//    /**
//     * 需要合并的列
//     */
//    private List<Integer> mergeColumnIndexList;
//    /**
//     * 从哪一行开始合并
//     */
//    private int startMergeRowIndex;
//
//    @Override
//    protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
//
//    }
//
//    public MyMergeStrategy(int startMergeRowIndex, List<Integer> mergeColumnIndexList) {
//        this.startMergeRowIndex = startMergeRowIndex;
//        this.mergeColumnIndexList = mergeColumnIndexList;
//    }
//
//    @Override
//    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
//        //当前行
//        int curRowIndex = cell.getRowIndex();
//        //当前列
//        int curColIndex = cell.getColumnIndex();
//
//        if (curRowIndex > startMergeRowIndex) {
//            for (Integer mergeColumnIndex : mergeColumnIndexList) {
//                if (curColIndex == mergeColumnIndex) {
//                    mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
//                    break;
//                }
//            }
//        }
//    }
//
//    /**
//     * 当前单元格向上合并
//     *
//     * @param writeSheetHolder
//     * @param cell             当前单元格
//     * @param curRowIndex      当前行
//     * @param curColIndex      当前列
//     */
//    private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
//        if (curRowIndex == 0){
//            return;
//        }
//        //获取当前行的当前列的数据和上一行的当前列列数据，通过上一行数据是否相同进行合并
//        Object curData = cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
//        Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
//        Object preData = preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();
//
//        // 比较当前行的第一列的单元格与上一行是否相同，相同合并当前单元格与上一行
//        if (curData.equals(preData)) {
//            Sheet sheet = writeSheetHolder.getSheet();
//            List<CellRangeAddress> mergeRegions = sheet.getMergedRegions();
//            boolean isMerged = false;
//            for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
//                CellRangeAddress cellRangeAddr = mergeRegions.get(i);
//                // 若上一个单元格已经被合并，则先移出原有的合并单元，再重新添加合并单元
//                if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
//                    sheet.removeMergedRegion(i);
//                    cellRangeAddr.setLastRow(curRowIndex);
//                    sheet.addMergedRegion(cellRangeAddr);
//                    isMerged = true;
//                }
//            }
//            // 若上一个单元格未被合并，则新增合并单元
//            if (!isMerged) {
//                CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
//                sheet.addMergedRegion(cellRangeAddress);
//            }
//        }
//    }
//}
